<?php

/**
 * Created by PhpStorm.
 * User: huangj11
 * Date: 2021/1/12
 * Time: 16:38
 */

namespace search\controllers\NewBigData;

use business\bigdata\services\queues\SupplierConsumerService;
use business\search\models\SupplierBigdataSearchForm;
use search\controllers\bigdata\BaseController;

class SupplierController extends BaseController
{

    /**
     * 供应商大数据搜索入口
     */
    public function actionIndex()
    {
        $searchForm = new SupplierBigdataSearchForm();
        $searchForm->attributes = $_GET;

        $resultSet = $searchForm->search();

        return $resultSet;
    }

    /**
     * 供应商搜索智能提示
     * @return array
     */
    public function actionSuggest()
    {
        $searchForm = new SupplierBigdataSearchForm();
        $searchForm->attributes = $_GET;

        $resultSet = $searchForm->getSuggester();

        return $resultSet;
    }

    public function actionDimName()
    {
        $searchForm = new SupplierBigdataSearchForm();
        $searchForm->attributes = $_GET;
        $resultSet = $searchForm->searchDimName();
        return $resultSet;
    }

    /**
     * 更新供应商索引
     * @param $supplierId
     * @return string
     */
    public function actionUpdateIndex($supplierId = null)
    {
        $result = ['error_code' => 0, 'message' => '更新成功'];
        if (empty($supplierId)) {
            $result['error_code'] = '1003';
            $result['message'] = '参数缺失';
            return $result;
        }
        $supplierService = new SupplierConsumerService();
        if ($supplierId == "all-data") {
            $command = 'php ' . \Yii::$app->getBasePath() . '/../protected/yii bigdata/supplier/full';
            $output = [];
            exec($command, $output);
            $result['message'] = '全量更新完毕.';
            return $result;
        }
        $supplierIds = explode(",", $supplierId);
        try {
            $supplierService->single($supplierIds);
            $result['message'] = '索引更新成功';
        } catch (\Exception $e) {
            $result = ['error_code' => 1004, 'message' => '索引更新失败'];
        }
        return $result;
    }
}